package com.moxiao.yfjh.module.order.controller.app;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeOrder.vo.BlindTradeOrderPageReqVO;
import com.moxiao.yfjh.module.order.controller.admin.blindTradeOrder.vo.BlindTradeOrderRespVO;
import com.moxiao.yfjh.module.order.controller.app.vo.*;
import com.moxiao.yfjh.module.order.convert.blindTradeOrder.BlindTradeOrderConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.blindTradeOrder.BlindTradeOrderDO;
import com.moxiao.yfjh.module.order.tradeRefund.blindTradeOrder.BlindTradeOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/22 18:50
 **/

@Tag(name = "APP - 相亲交易订单")
@RestController
@RequestMapping("/order/blind/trade")
@Validated
@Slf4j
public class AppBlindOrderController {

    @Resource
    private BlindTradeOrderService blindTradeOrderService;
    @Resource
    private BlindMemberUserService blindMemberUserService;


    @PostMapping("/createBlindMemberOrder")
    @Operation(summary = "提交会员支付订单")
    public CommonResult<Object> createBlindMemberOrder(@Valid @RequestBody AppBlindOrderCreateReqVO appBlindOrderCreateReqVO) {
        appBlindOrderCreateReqVO.setUserId(getLoginUserId());
        Object jsonObject = blindTradeOrderService.createOrder(appBlindOrderCreateReqVO);
        if (appBlindOrderCreateReqVO.getDeviceType() == 1) {
            return success(jsonObject);
        } else {
            return success(Boolean.TRUE);
        }
    }


    @PostMapping("/createBlindRecommendOrder")
    @Operation(summary = "提交超级推荐支付订单")
    public CommonResult<Object> createBlindRecommendOrder(@Valid @RequestBody AppRecommendOrderReqVO appRecommendOrderReqVO) {

        appRecommendOrderReqVO.setUserId(getLoginUserId());
        Object jsonObject = blindTradeOrderService.createRecommendOrder(appRecommendOrderReqVO);
        if (appRecommendOrderReqVO.getDeviceType() == 1) {
            return success(jsonObject);
        } else {
            return success(Boolean.TRUE);
        }
    }


    @PostMapping("/createBlindMakerOrder")
    @Operation(summary = "提交红娘服务支付订单")
    public CommonResult<Object> createBlindMakerOrder(@Valid @RequestBody AppMakerOrderReqVO appRecommendOrderReqVO) {
        appRecommendOrderReqVO.setUserId(getLoginUserId());
        Object jsonObject = blindTradeOrderService.createBlindMakerOrder(appRecommendOrderReqVO);
        if (appRecommendOrderReqVO.getDeviceType() == 1) {
            return success(jsonObject);
        } else {
            return success(Boolean.TRUE);
        }
    }


    @PostMapping("/createActivityOrder")
    @Operation(summary = "提交活动支付订单")
    public CommonResult<AppBlindOrderCreateRespVO> createActivityOrder(@Valid @RequestBody AppActivityOrderReqVO appActivityOrderReqVO) {
        appActivityOrderReqVO.setUserId(getLoginUserId());
        return success(blindTradeOrderService.createActivityOrder(appActivityOrderReqVO));
    }


    @GetMapping("/getBlindTradeOrder")
    @Operation(summary = "获得相亲交易订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlindTradeOrderRespVO> getBlindTradeOrder(@RequestParam("id") Long id) {
        BlindTradeOrderDO blindTradeOrder = blindTradeOrderService.getBlindTradeOrder(id);
        return success(BlindTradeOrderConvert.INSTANCE.convert(blindTradeOrder));
    }


    @GetMapping("/getBlindTradeOrderPage")
    @Operation(summary = "获得相亲交易订单分页")
    public CommonResult<PageResult<BlindTradeOrderRespVO>> getBlindTradeOrderPage(@Valid BlindTradeOrderPageReqVO pageVO) {
        pageVO.setUserId(getLoginUserId());
        PageResult<BlindTradeOrderDO> pageResult = blindTradeOrderService.getBlindTradeOrderPage(pageVO);
        PageResult<BlindTradeOrderRespVO> voPageResult = BlindTradeOrderConvert.INSTANCE.convertPage(pageResult);
        if (Objects.nonNull(voPageResult)) {
            List<BlindTradeOrderRespVO> list = voPageResult.getList();
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(blindTradeOrderRespVO -> {
                    Long userId = blindTradeOrderRespVO.getUserId();
                    BlindMemberUserDO memberUser = blindMemberUserService.getMemberUser(userId);
                    if (Objects.nonNull(memberUser)) {
                        blindTradeOrderRespVO.setMemberEndTime(memberUser.getMemberEndTime());
                    }
                });
            }
        }
        return success(voPageResult);
    }


    @DeleteMapping("/deleteBlindTradeOrder")
    @Operation(summary = "删除相亲交易订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBlindTradeOrder(@RequestParam("id") Long id) {
        blindTradeOrderService.deleteBlindTradeOrder(id);
        return success(true);
    }


}
